package com.youi.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youi.shop.commons.result.RespEntity;
import com.youi.shop.commons.result.StatusCode;
import com.youi.shop.mapper.CartMapper;
import com.youi.shop.mapper.ProductMapper;
import com.youi.shop.pojo.entity.Cart;
import com.youi.shop.pojo.entity.Product;
import com.youi.shop.pojo.qo.cart.CartInsertQO;
import com.youi.shop.pojo.vo.cart.CartVO;
import com.youi.shop.pojo.vo.product.ProductVO;
import com.youi.shop.service.CartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    CartMapper cartMapper;
    @Resource
    ProductMapper productMapper;

    /**
     * 根据会员id查询购物车信息
     *
     * @param memberId
     * @return
     */
    @Override
    public RespEntity<List<CartVO>> selectByMemberId(Long memberId) {
        //根据会员id查询购物车列表
        List<Cart> cartList = cartMapper.selectByMemberId(memberId);
        if (cartList == null || cartList.size() == 0) {
            return RespEntity.error(StatusCode.CART_NULL_ERROR);
        }
        List<CartVO> list = new ArrayList<>();
        CartVO cartVO = null;

        //循环遍历存入购物车vo
        for (Cart cart : cartList) {
            cartVO = new CartVO();
            cartVO.setCartId(cart.getCartId());
            cartVO.setMemberId(cart.getMemberId());
            cartVO.setProductId(cart.getProductId());
            cartVO.setQuantity(cart.getQuantity());
            cartVO.setTotalAmount(cart.getTotalAmount());
            //商品vo存入cartvo
            ProductVO productVO = new ProductVO();
            Product product = productMapper.selectById(cartVO.getProductId());
            BeanUtils.copyProperties(product,productVO);
            cartVO.setProductVO(productVO);

            list.add(cartVO);
        }

        return RespEntity.success(list);
    }


    /**
     * 将单个商品添加到购物车
     *
     * @param cartInsertQO
     * @return
     */
    @Override
    public RespEntity<List<CartVO>> addOne(CartInsertQO cartInsertQO) {
        //判断商品是否有添加到购物车中
        Cart cart = cartMapper.selectByProductIdAndMemberId(cartInsertQO.getProductId(), cartInsertQO.getMemberId());
        int res = 0;
        //存在就更新该购物车的商品数量
        if (cart != null) {
            cart.setQuantity(cart.getQuantity() + 1);
            cart.setTotalAmount(cart.getTotalAmount().add(cart.getProduct().getProductPrice()));
            res = cartMapper.updateById(cart);

        } else {
            //不存在就查询该商品对象
            //查询商品id是否存在
            Product product = productMapper.selectById(cartInsertQO.getProductId());
            //创建新的cart
            cart = new Cart();
            cart.setProductId(cartInsertQO.getProductId());
            cart.setMemberId(cartInsertQO.getMemberId());
            cart.setProduct(product);
            cart.setQuantity(1);
            cart.setTotalAmount(product.getProductPrice());

            //插入cart
            res = cartMapper.insert(cart);
        }
        //插入成功
        if (res > 0) {
            //查询该会员的所有购物车信息
            QueryWrapper<Cart> wrapper = new QueryWrapper<>();
            wrapper.eq("member_id", cartInsertQO.getMemberId());
            List<Cart> cartList = cartMapper.selectList(wrapper);
            //给客户端发送更新后的数据
            CartVO cartVO = null;


            List<CartVO> list = new ArrayList<>();
            //给vo赋值
            for (Cart cart1 : cartList) {
                cartVO = new CartVO();
                cartVO.setCartId(cart1.getCartId());
                cartVO.setMemberId(cart1.getMemberId());
                cartVO.setProductId(cart1.getProductId());
                cartVO.setQuantity(cart1.getQuantity());
                cartVO.setTotalAmount(cart1.getTotalAmount());
                ProductVO productVO = new ProductVO();
                Product product = productMapper.selectById(cart1.getProductId());
                BeanUtils.copyProperties(product, productVO);
                cartVO.setProductVO(productVO);

                list.add(cartVO);

            }


            return RespEntity.success(list);
        }
        //插入失败
        return null;
    }

    /**
     * 根据id查询
     * @param cartId
     * @return
     */
    @Override
    public Cart selectByCartId(Long cartId) {


        return cartMapper.selectByCartId(cartId);
    }


}
